import { systemAppStore } from "@/stores/system";
import { userAppStore } from "@/stores/user";
import router from "./router";
const whiteList: string[] = [];
const whiteList2: string[] = [];
router.beforeEach(async (to, from, next) => {
  if (whiteList.includes(to.path)) {
    return next();
  }
  if (!systemAppStore().appInfo.uuid) {
    await systemAppStore().getDeviceInfo();
  }
  if (whiteList2.includes(to.path)) {
    return next();
  }
  if (!userAppStore().token) {
    await userAppStore().getToken();
  }
  if (userAppStore().token && !userAppStore().hasLogin) {
    await userAppStore()
      .checkStatus()
      .catch(() => {});
  }

  if (systemAppStore().appInfo.merchant === 'lx') {
    await userAppStore().getLxToken().catch(() => {});
    await userAppStore().getPayConfig().catch(() => {});
  }

  if (to.meta.noNeedLogin) {
    return next();
  }

  if (!userAppStore().hasLogin && !to.meta.noNeedLogin) {
    if (!from.meta.noNeedLogin && from.path !== "/") {
      next({
        path: "/",
        query: to.query
      });
    }
    userAppStore().openLogin();
    return;
  }
  next();
});
